1 package cz
.brmlab
.brmtron
.server
;
6 public class ProtocolHelper
{
8 //id --- name --- longitude --- latitude --- active\n
10 private static final int FIELD_ID
= 0;
11 private static final int FIELD_NAME
= 1;
12 private static final int FIELD_LONGITUDE
= 2;
13 private static final int FIELD_LATITUDE
= 3;
14 private static final int FIELD_ACTIVE
= 4;
15 private static final String FIELD_DELIMITER
= " --- ";
17 public static Player
parsePlayer(String input
) {
19 String
[] tmp
= input
.trim().split(FIELD_DELIMITER
);
20 int id
= Integer
.parseInt(tmp
[FIELD_ID
]);
21 Player ret
= new Player(id
);
22 ret
.setName(tmp
[FIELD_NAME
]);
23 ret
.setCoords(Double
.parseDouble(tmp
[FIELD_LONGITUDE
]), Double
.parseDouble(tmp
[FIELD_LATITUDE
]));
24 ret
.setActive(Boolean
.parseBoolean(tmp
[FIELD_ACTIVE
]));
28 public static String
buildPlayer(Player p
) {
29 StringBuilder sb
= new StringBuilder();
32 sb
.append(FIELD_DELIMITER
);
33 sb
.append(p
.getName());
34 sb
.append(FIELD_DELIMITER
);
35 sb
.append(p
.getLongitude());
36 sb
.append(FIELD_DELIMITER
);
37 sb
.append(p
.getLatitude());
38 sb
.append(FIELD_DELIMITER
);
39 sb
.append(p
.isActive());
47 * @param players Numbered list of players
48 * @return String representation of each player's state
50 public static String
getUpdate(Map
<Integer
, Player
> players
) {
51 StringBuilder sb
= new StringBuilder();
52 Set
<Integer
> keys
= players
.keySet();
55 sb
.append(buildPlayer(players
.get(i
)));